package com.gpay.erp.services.common.impl;

import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.SystemMaxNumber;
import com.gpay.erp.services.common.SystemCommonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by wuzonghui on 15/11/6.
 */
@Service
public class SystemCommonServiceImpl implements SystemCommonService {
    @Resource
    private BaseDao dao;
    @Override
    public int insert(SystemMaxNumber systemMaxNumber) {
        return dao.insert("systemMaxNumber.insert",systemMaxNumber);
    }

    @Override
    public int delete(SystemMaxNumber systemMaxNumber) {
        return 0;
    }

    @Override
    public int deletes(String[] ids) {
        return 0;
    }

    @Override
    public int update(SystemMaxNumber systemMaxNumber) {
        return dao.update("systemMaxNumber.updateByCode",systemMaxNumber);
    }

    @Override
    public SystemMaxNumber selectOne(SystemMaxNumber systemMaxNumber) {
        return null;
    }

    @Override
    public SystemMaxNumber selectById(String id) {
        return (SystemMaxNumber) dao.selectOne("systemMaxNumber.selectById",id);
    }

    @Override
    public PagerModel selectPageList(SystemMaxNumber systemMaxNumber) {
        return null;
    }

    @Override
    public List<SystemMaxNumber> selectList(SystemMaxNumber systemMaxNumber) {
        return null;
    }

    /**
     * 最大值规格
     * 程序首字母开头一个单词
     * 中间六位 yyMM
     * 后三位 00000开始的数据
     * 后面是最大值
     * @param maxCode
     * @return
     */
    @Override
    public String saveSelectByName(String maxCode){
        StringBuffer maxNumbersb = new StringBuffer(""); //
        SystemMaxNumber systemMaxNumber = (SystemMaxNumber) dao.selectOne("systemMaxNumber.selectByCode",maxCode);
        if(systemMaxNumber !=null){
            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            DateFormat format1 = new SimpleDateFormat("yyMM");
            String s = format1.format(date);
            String maxNumbers = systemMaxNumber.getMaxNumbers();

            String splitMax = maxNumbers.substring(1,5);
            if(s.equals(splitMax)) { //判断当前的年月
                maxNumbersb.append(maxNumbers.substring(0,5));
                String nums = maxNumbers.substring(5);
                String result = String.valueOf(Integer.parseInt(nums)+1);//当前的流水号+1
                int size = 5-result.length();
                for(int j=0;j<size;j++){
                    result="0"+result;
                }
                maxNumbersb.append(result);

            }else { //新月第一条数据
                String strNew = maxNumbers.substring(0,1);
                maxNumbersb.append(strNew+s+"00001");
            }
            systemMaxNumber.setMaxNumbers(maxNumbersb.toString()); //更新表中最大值
            update(systemMaxNumber);
        }
        return maxNumbersb.toString();
    }

    /**
     * 获取子项最大直
     * @param maxCode
     * @return
     */
    @Override
    public String saveSubSelectByName(String maxCode){
        StringBuffer maxNumbersb = new StringBuffer(""); //
        SystemMaxNumber systemMaxNumber = (SystemMaxNumber) dao.selectOne("systemMaxNumber.selectByCode",maxCode);
        if(systemMaxNumber !=null){
            String subMaxCode = systemMaxNumber.getSubMaxNumbers();
                maxNumbersb.append(subMaxCode.substring(0,10));
                String nums = subMaxCode.substring(10);
                String result = String.valueOf(Integer.parseInt(nums)+1);//当前的流水号+1
                int size = 3-result.length();
                for(int j=0;j<size;j++){
                    result="0"+result;
                }
                maxNumbersb.append(result);

            systemMaxNumber.setSubMaxNumbers(maxNumbersb.toString()); //更新表中最大值
            update(systemMaxNumber);
        }
        return maxNumbersb.toString();
    }
    /**
     * 最大值规格
     * 程序首字母开头两个单词
     * 中间四位 yyMM
     * 后三位 00000开始的数据
     * 后面是最大值
     * @param maxCode
     * @return
     */
    @Override
    public String saveTwoSelectByName(String maxCode){
        StringBuffer maxNumbersb = new StringBuffer(""); //
        SystemMaxNumber systemMaxNumber = (SystemMaxNumber) dao.selectOne("systemMaxNumber.selectByCode",maxCode);
        if(systemMaxNumber !=null){
            Calendar calendar = Calendar.getInstance();
            Date date = calendar.getTime();
            DateFormat format1 = new SimpleDateFormat("yyMM");
            String s = format1.format(date);
            String maxNumbers = systemMaxNumber.getMaxNumbers();

            String splitMax = maxNumbers.substring(2,6);
            if(s.equals(splitMax)) { //判断当前的年月
                maxNumbersb.append(maxNumbers.substring(0,6));
                String nums = maxNumbers.substring(6);
                String result = String.valueOf(Integer.parseInt(nums)+1);//当前的流水号+1
                int size = 5-result.length();
                for(int j=0;j<size;j++){
                    result="0"+result;
                }
                maxNumbersb.append(result);

            }else { //新月第一条数据
                String strNew = maxNumbers.substring(0,1);
                maxNumbersb.append(strNew+s+"00001");
            }
            systemMaxNumber.setMaxNumbers(maxNumbersb.toString()); //更新表中最大值
            update(systemMaxNumber);
        }
        return maxNumbersb.toString();
    }

    /**
     * 获取子项最大直
     * @param maxCode
     * @return
     */
    @Override
    public String saveSubTwoSelectByName(String maxCode){
        StringBuffer maxNumbersb = new StringBuffer(""); //
        SystemMaxNumber systemMaxNumber = (SystemMaxNumber) dao.selectOne("systemMaxNumber.selectByCode",maxCode);
        if(systemMaxNumber !=null){
            String subMaxCode = systemMaxNumber.getSubMaxNumbers();
            maxNumbersb.append(subMaxCode.substring(0,11));
            String nums = subMaxCode.substring(11);
            String result = String.valueOf(Integer.parseInt(nums)+1);//当前的流水号+1
            int size = 3-result.length();
            for(int j=0;j<size;j++){
                result="0"+result;
            }
            maxNumbersb.append(result);

            systemMaxNumber.setSubMaxNumbers(maxNumbersb.toString()); //更新表中最大值
            update(systemMaxNumber);
        }
        return maxNumbersb.toString();
    }
}
